package site.xuxingcan;

import com.google.gson.Gson;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerUtil {
    public static ServerSocket serverSocket = null;
    private static Gson gson = new Gson();

    public static void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    serverSocket = new ServerSocket(8191);
                    while (true) {
                        final Socket socket = serverSocket.accept();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    SocketHelper socketHelper = new SocketHelper(socket);
                                    Gson gson = new Gson();
                                    while (true) {
                                        String s = socketHelper.readLine();
                                        if (s == null || s.length() == 0) {
                                            break;
                                        }
                                        Model model = gson.fromJson(s, Model.class);
                                        if (model == null) {
                                            continue;
                                        }
                                        if ("heartBeat".equalsIgnoreCase(model.msg)) {
                                            socketHelper.writeLineAndFlush("heartBeat");
                                            continue;
                                        }
                                        if (model.msg == null || model.msg.length() == 0) {
                                            if (model.Token != null && model.Token.length() != 0
                                                    && model.userId != null && model.userId.length() != 0
                                                    && model.zbShipmentId != null && model.zbShipmentId.length() != 0) {
                                                socketHelper.writeLineAndFlush("heartBeat");
                                                continue;
                                            }
                                        }
                                        socketHelper.writeLineAndFlush("nothing");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void stop() {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
